package com.attilax.io;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.FileUtils;

public class FileService {
	
	public static void main(String[] args) throws IOException {
		String oriname="C:\\0wkspc\\hislog\\src\\main\\java\\com\\attilax\\io\\filenameEncodeTest.txt";
		String t=FileUtils.readFileToString(new File(oriname));
		String newfilename=fileNameEncode_readableBest(t);
		FileUtils.writeStringToFile(new File("C:\\0wkspc\\hislog\\src\\main\\java\\com\\attilax\\io\\"+newfilename+".txt"), "data");
	System.out.println("--");
	}

/**
 * but cant convert to ori	
 * @param filenameOri
 * @return
 */
	public static String fileNameEncode_readableBest(String filenameOri) {
		filenameOri=filenameOri.replaceAll("\\\\", "、");
	 	filenameOri=filenameOri.replaceAll("\\/", "、"); 
		filenameOri=filenameOri.replaceAll("\\|", "、"); 
		filenameOri=filenameOri.replaceAll("\\*", "。"); 
		filenameOri=filenameOri.replaceAll("\\?", "？"); 
		filenameOri=filenameOri.replaceAll("\"", "“"); 
		filenameOri=filenameOri.replaceAll("<", "《"); 
			filenameOri=filenameOri.replaceAll(">", "》"); 
	 		filenameOri=filenameOri.replaceAll(":", "："); 
		return filenameOri;
	}
	
//	
//	public static String fileNameEncode(String filenameOri) {
//		// /\:* <>\"|
//		if (filenameOri.equals("."))
//			return "%2E";
//		if (filenameOri.equals(".."))
//			return "%2E%2E";
//		Map<String, String> mp = (Map<String, String>) new ClosureNoExcpt() {
//
//			@Override
//			public Object execute(final Object arg0) {
//				final Map<String, String> mp = new HashMap<String, String>() {
//					{
//						this.put("*", "%2A");
//
//					}
//				};
//				final String[] as = strUtil.SplitByNone("/\\:?<>\"|");
//
//				for (final String s : as) {
//					try {
//						mp.put(s, URLEncoder.encode(s, "utf-8"));
//
//					} catch (final UnsupportedEncodingException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//				return mp;
//			}
//		}.execute(null);
//		String[] as = strUtil.SplitByNone(filenameOri);
//		String fname2 = "";
//		for (String s : as) {
//			fname2 += mp.get(s) == null ? s : mp.get(s);
//		}
//		return fname2;
//	}

	public static void exists_waitforGeneFile(String sqlpath, int timeoutMills) {

		int pasttime = 0;
		while (true) {
			if (new File(sqlpath).exists())
				break;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pasttime = pasttime + 1000;
			if (pasttime > timeoutMills)
				throw new RuntimeException("timeout:" + timeoutMills);
		}

	}

	public static void writeByteArrayToFile(String pathname, byte[] data_bytearr) {
		try {
			  FileUtils.forceMkdir(new File(pathname).getParentFile());
			FileUtils.writeByteArrayToFile(new File(pathname), data_bytearr);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		 
      
		
	}
	
	public static void move(String f, String targetDir, String oriDir) {
		//   int splt=f.indexOf(oriDir);
		f=f.trim();
		   String rltFilename=f.substring(oriDir.length()+1);
		   String newname=targetDir+File.separator+rltFilename;
		   if(new File(newname).exists())
			   new File(newname).delete();
		   move(f,newname);
		
	}
	
	/**
	@author attilax 闁奸绀侀幖浼存儍閸曨厼鐒婚悗娑虫嫹
		@since  2014-5-8 濞戞挸锕ゅ畷锟�9:37:40$
	
	 * @param f
	 * @param string
	 */
	public static boolean move(String f, String target) {
		// attilax 閼颁礁鎼伴惃鍕焻鐎涳拷  娑撳﹤宕�9:37:40   2014-5-8 
//		File f=new  File(target);
//		if(f.exists())
		if(!target.contains("."))
			throw new RuntimeException("target is not file path,maiby only dir:"+target);
			createAllPath(target);
			
			File oldFile = new File(f);
			//filex.move(f,target);
//			
//			// 鐏忓棙鏋冩禒鍓佇╅崚鐗堟煀閺傚洣娆㈤柌锟�
//			
 		File fnew = new File(target);
 	return	oldFile.renameTo(fnew);
		
		
	}
	
	/**
	 * o3i    
	 * @param fileFullPath 
	 */
	public static void createAllPath(String fileFullPath) {
		File file = new File(fileFullPath);
		if(file.getParentFile().exists())
			if(file.getParentFile().isFile())
				throw new RuntimeException("exist same name file: should be create dir for file:"+fileFullPath);
		 if (!file.getParentFile().exists()) {
		   //  System.out.println("閻╊喗鐖ｉ弬鍥︽閹碉拷閸︺劏鐭惧鍕瑝鐎涙ê婀敍灞藉櫙婢跺洤鍨卞鎭掞拷鍌橈拷鍌橈拷锟�+fileFullPath);
		     if (!file.getParentFile().mkdirs()) {
		    	 throw new RuntimeException("create parent  dir fail:"+fileFullPath);
		   //   System.out.println("閸掓稑缂撻惄顔肩秿閺傚洣娆㈤幍锟介崷銊ф畱閻╊喖缍嶆径杈Е閿涳拷"+fileFullPath);
		   
		     }
		 }
	}

}
